<?php
if ( defined('ABSPATH') )
	include_once ABSPATH . 'config.php';
else
    include_once '../config.php';

include_once ABSPATH."base/people.php";
include_once ABSPATH."base/curl.php";
include_once ABSPATH."base/sig_manager.php";
include_once ABSPATH."common/funclib.php";

//Commands
//secret.get - Get shared secret from address for me.
//group.secret.get - Get shared secret from address for group.
//feed.public - Retrieve public feed.
//feed.me - Retrieve feed for me using secret.
//feed.group - Retrieve feed for group using secret.
//profile - Get profile for address.

//secret.post - Post secret to address from me.
//group.secret.post - Post secret to address from me for group.

$cmd = "";
$id = "";
$from = "";
$sig = "";
$last_update;


if(isset($_REQUEST["cmd"]))
{
	$cmd = $_REQUEST["cmd"];
}

if(isset($_REQUEST["id"]))
{
	$id=$_REQUEST["id"];
}
if(isset($_REQUEST["from"]))
{
	$from=$_REQUEST["from"];
	$fromID = Person::IDFromAddress($from);
	if(!$fromID)
	{
		if($cmd == "feed.personal")
		{
			$cmd = "feed.public";
		}
		else
		{
			echo 'Sorry.  You are not connected to this person.';
			return;
		}
	}
}

if(isset($_REQUEST["lastupdate"]))
{
	$last_update = $_REQUEST["lastupdate"];
}



if(isset($_REQUEST["sig"]))
{
	$sig=$_REQUEST["sig"];
}

if(isset($_REQUEST["screen_name"]))
{
	$screen_name = $_REQUEST["screen_name"];
    $id = Person::IDFromScreenName($screen_name);
}


if(isset($_REQUEST["address"]))
{
	$address = $_REQUEST["address"];
    $id = Person::IDFromAddress($address);
}

$person = new Person($id);
$person->Get();

if(strlen($cmd) > 0)
{
	switch($cmd)
	{
		case "verify":
			$sigmgr = new Signature($id,$from);
			$rs = $sigmgr->Get();
			$found = false;
			if($rs)
			{
				foreach($rs as $row)
				{
					if($row['signature'] == $sig)
					{
						$found = true;
					}
				}
			}

			if($found)
			{
				echo 'ok';
			}
			else
			{
				echo 'failed';
			}
			exit;
			break;		
		case "feed.public":
			$cmd = "feed.public";
			break;
		case "feed.personal":
			$cmd = "feed.personal";
			break;
		case "feed.group":
			break;
		case "profile":
			$cmd = "profile";
			break;
		case "profile.xml":
			$cmd = "profile.xml";
			break;
			
	}
}

$curl = new cURL(FALSE);

if($cmd == "feed.public")
{
	$url = HOME."feed.php?id=$id&lastupdate=$last_update";
	$xml =  $curl->get($url);
	echo $xml;
}

if($cmd == "feed.personal")
{
	$url = HOME."feed.php?id=$id&from=$from&sig=$sig&lastupdate=$last_update";
	$xml =  $curl->get($url);
	echo $xml;
}



if($cmd == "profile") :
?>
<html>
<head>
</head>
<body>
<p><?php echo $person->name; ?></p>
<p><?php echo $person->screen_name; ?></p>
<p><?php echo '<img src="'.$person->image.'"/>'; ?></p>
</body>
</html>
<?php
endif;

if($cmd == "profile.xml")
{
    echo "<user><name>$person->name</name><screen_name>$person->screen_name</screen_name><address>$person->address</address>";
    echo "<profile_image_url>$person->image</profile_image_url></user>";	
}

?>